package com.example.mapper;

import com.example.pojo.DataInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface HistoryDataMapper {

    /**
     * 按小时聚合温度数据
     */
    void aggregationDataByHour();

    /**
     * 按天聚合温度数据
     */
    void aggregationDataByDay();

    /**
     * 按月聚合温度数据
     */
    void aggregationDataByMonth();

    /**
     * 插入数据
     */
    @Insert("insert into temp_humi_data(device_id, temperature, humidity) values(#{deviceId}, #{temperature}, #{humidity})")
    void insertData(DataInfo dataInfo);

    /**
     * 获取某一天的温度历史数据
     */
    @Select("select DATE_FORMAT(stat_time, '%H:%i:%s') as time, avg_temperature as data from temp_humi_hourly_data where stat_time > #{startTime} and stat_time < #{endTime};")
    List<Map<String, Object>> getTemperatureDataByHour(LocalDateTime startTime, LocalDateTime endTime);

    /**
     * 获取某个时间段的日温度历史数据
     */
    @Select("select DATE_FORMAT(stat_time, '%m-%d') as time, avg_temperature as data from temp_humi_daily_data where stat_time >= #{startTime} and stat_time <= #{endTime}")
    List<Map<String, Object>> getTemperatureDataByDay(LocalDate startTime, LocalDate endTime);

    /**
     * 获取某一年的月温度历史数据
     */
    @Select("select DATE_FORMAT(stat_time, '%Y-%m') as time, avg_temperature as data from temp_humi_monthly_data where stat_time >= #{startTime} and stat_time <= #{endTime}")
    List<Map<String, Object>> getTemperatureDataByMonth(LocalDate startTime, LocalDate endTime);

    /**
     * 获取某一天的湿度历史数据
     */
    @Select("select DATE_FORMAT(stat_time, '%H:%i:%s') as time, avg_humidity as data from temp_humi_hourly_data where stat_time > #{startTime} and stat_time < #{endTime};")
    List<Map<String, Object>> getHumidityDataByHour(LocalDateTime startTime, LocalDateTime endTime);

    /**
     * 获取某个时间段的日湿度历史数据
     */
    @Select("select DATE_FORMAT(stat_time, '%m-%d') as time, avg_humidity as data from temp_humi_daily_data where stat_time >= #{startTime} and stat_time <= #{endTime}")
    List<Map<String, Object>> getHumidityDataByDay(LocalDate startTime, LocalDate endTime);

    /**
     * 获取某一年的月湿度历史数据
     */
    @Select("select DATE_FORMAT(stat_time, '%Y-%m') as time, avg_humidity as data from temp_humi_monthly_data where stat_time >= #{startTime} and stat_time <= #{endTime}")
    List<Map<String, Object>> getHumidityDataByMonth(LocalDate startTime, LocalDate endTime);
}
